import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


public class Leetcode1380 {
    public static void main(String[] args) {
        System.out.println(luckyNumbers(new int[][]{
                {1, 10, 4, 2}, {9, 3, 8, 7}, {15, 16, 17, 12}
        }));
    }

    public static List<Integer> luckyNumbers(int[][] matrix) {
        List<Integer> rowMin = new ArrayList<>();
        for (int[] row : matrix) {
            int min = Integer.MAX_VALUE;
            for (int column : row) {
                if (column < min) {
                    min = column;
                }
            }
            rowMin.add(min);
        }

        int n = matrix[0].length;
        List<Integer> columnMax = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            int max = Integer.MIN_VALUE;
            for (int[] ints : matrix) {
                if (ints[i] > max) {
                    max = ints[i];
                }
            }
            columnMax.add(max);
        }

        Set<Integer> set = new HashSet<>(rowMin);
        for (Integer max : columnMax) {
            if (set.contains(max)) {
                return List.of(max);
            }
        }

        return List.of();
    }

}
